home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Stack / Base_Stack.h < prev    next >
C/C++ Source or Header  |  1992-04-13  |  5KB  |  136 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: VDN 02/21/92 -- New lite version
  14. //
  15. // The CoolStack class is  publicly  derived from the  Generic class and is used to
  16. // implement non-type specific functionality for the parameterized CoolStack class.
  17. // In  this manner, code common  to  all instances  of the  CoolStack  class can be
  18. // shared to reduce code replication.  The  CoolStack<Type> class  implements a one
  19. // dimensional vector of a user-specified type.  This  is accomplished by using
  20. // the parameterized type  capability of C++.  The  stack will grow dynamically
  21. // as necessary  with   the amount  of  growth determined  by  the value  of an
  22. // allocation size slot.  Fixed length stacks are also supported by setting the
  23. // value of the allocation size slot to zero.
  24. //
  25. // Each CoolStack object contains a protected data section that has  a slot to hold
  26. // the current size of  the  stack,  a slot to  thold  the  number of  elements
  27. // currently on the  stack,  a static specificying  the allocation growth size,
  28. // and a float that may be set to some percentage to specify a growth ratio for
  29. // this instance of a queue
  30. //
  31. // There are three  constructors for the   CoolStack class.  The  first constructor
  32. // takes no arguments and creates an empty CoolStack object of the  specified type.
  33. // The second constructor takes an argument specifying the initial size  of the
  34. // stack.  Finally, the third constructor takes a single argument consisting of
  35. // a reference to a CoolStack and duplicates its size and values.
  36. //
  37. // The CoolStack class  provides generic, type-independent  methods to  remport the
  38. // number of items in  the stack, check the empty  status,  and clear all items
  39. // from the stack. The assignment operator is  overloaded and the three methods
  40. // to set the allocation growth size for the class as a whole, the growth ratio
  41. // for a specific instance of  a stack, and the length  (ie. the  largest valid
  42. // zero-relative index for random access) of the  stack are available. Finally,
  43. // exception handling functions  called by the parameterized CoolStack<Type>  class
  44. // are located   in the  base  class  to  facilitate code    charing  of common
  45. // functionality.
  46. //
  47.  
  48. #ifndef BASE_STACKH        // If no definition for CoolStack
  49. #define BASE_STACKH        // Define stack symbol
  50.  
  51. #ifndef STREAMH            // If the Stream support not yet defined,
  52. #if defined(DOS) || defined(M_XENIX)
  53. #include <stream.hxx>           // include the Stream class header file
  54. #else
  55. #include <stream.h>        // include the Stream class header file
  56. #endif
  57. #define STREAMH
  58. #endif
  59.  
  60. #ifndef MISCELANEOUSH        // If we have not included this file,
  61. #include <misc.h>        // include miscelaneous useful definitions.
  62. #endif
  63.  
  64. #define STACK_MEM_BLK_SZ 100
  65.  
  66. class CoolStack {
  67. protected:
  68.   long size;                    // Size of allocated storage
  69.   long number_elements;                // Number of elements in stack
  70.   static alloc_size_s;                // Allocation size for growth
  71.   float growth_ratio;                // If non-zero, growth ratio
  72.  
  73.   void top_error (const char*);            // Raise exception
  74.   void pop_error (const char*);            // Raise exception
  75.   void bracket_error (const char*, long);    // Raise exception
  76.   void push_error (const char*);        // Raise exception
  77.   void popn_error (const char*, long);        // Raise exception
  78.   void resize_error (const char*, long);    // Raise exception
  79.   void assign_error (const char*);        // Raise exception
  80.  
  81. public:
  82.   CoolStack ();                    // Simple constructor
  83.   CoolStack (long);                    // CoolStack of initial size
  84.   CoolStack (const CoolStack&);                // Duplicate another stack
  85.   ~CoolStack ();                    // CoolStack destructor
  86.  
  87.   CoolStack& operator= (const CoolStack& s);        // Assignment s = s2;
  88.  
  89.   inline Boolean is_empty () const;        // Is stack empty?
  90.   inline void clear ();                // Clears all values from stack
  91.   inline long length () const;            // Return number of stack items
  92.   inline long capacity() const;            // Max. number of elements
  93.  
  94.   long set_length (long, const char*);        // Set number of elements
  95.   void set_growth_ratio (float, const char*);    // Set growth percentage
  96.   void set_alloc_size (int, const char*);    // Set alloc size
  97. };
  98.  
  99.  
  100. // long length() -- Return the number of elements in this stack
  101. // Input:           None
  102. // Output:          Integer representing number of elements
  103.  
  104. inline long CoolStack::length () const {
  105.   return this->number_elements;
  106. }
  107.  
  108. // capacity -- Return maximum number of elements object can hold
  109. // Input:      None
  110. // Output:     Integer value of maximum number of elements
  111.  
  112. inline long CoolStack::capacity () const {
  113.   return (this->size);                // Return max number of values
  114. }
  115.  
  116.  
  117. // Boolean is_empty() -- Return TRUE if this stack is empty
  118. // Input:                None
  119. // Output:               TRUE or FALSE
  120.  
  121. inline Boolean CoolStack::is_empty () const {
  122.   return (this->number_elements == 0 ? TRUE : FALSE);
  123. }
  124.  
  125.  
  126. // void clear() -- Empty this stack  
  127. // Input:          None
  128. // Output:         None
  129.  
  130. inline void CoolStack::clear () {
  131.   this->number_elements = 0;
  132. }
  133.  
  134. #endif                        // End BASE_STACKH
  135.  
  136.